package cn.rick.controller;

import cn.dev33.satoken.util.SaResult;
import cn.rick.entity.BuildCategory;
import cn.rick.entity.BuildMaterials;
import cn.rick.service.IBuildCategoryService;
import cn.rick.service.IBuildMaterialsService;
import cn.rick.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 主材料表 前端控制器
 * </p>
 *
 * @since 2022-11-03 08:42:42
 */
@RestController
@RequestMapping("/api/buildMaterials")
public class BuildMaterialsController {

    @Resource
    private IBuildMaterialsService iBuildMaterialsService;

    @Resource
    private IBuildCategoryService iBuildCategoryService;

    @GetMapping("/getData")
    @ApiOperation("获取所有主材料数据")
    public SaResult getData(BuildMaterials buildMaterials, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<BuildMaterials> query = new MPJLambdaWrapper<>();
        query.innerJoin(BuildCategory.class, BuildCategory::getId, BuildMaterials::getCategory);
        query.selectAll(BuildMaterials.class);
        query.selectAs(BuildCategory::getName, BuildMaterials::getCategoryName);
        query.orderByDesc(BuildMaterials::getCreateTime);

        if (StringUtils.isNotEmpty(buildMaterials.getName())) {
            query.like(BuildMaterials::getName, buildMaterials.getName());
        }
        if (StringUtils.isNotNull(buildMaterials.getSupplier())) {
            query.eq(BuildMaterials::getSupplier, buildMaterials.getSupplier());
        }
        if (StringUtils.isNotEmpty(buildMaterials.getSupplierArticleNo())) {
            query.like(BuildMaterials::getSupplierArticleNo, buildMaterials.getSupplierArticleNo());
        }
        if (StringUtils.isNotNull(buildMaterials.getCategory())) {
            query.eq(BuildMaterials::getCategory, buildMaterials.getCategory());
        }

        PageHelper.startPage(pageNum, pageSize);
        List<BuildMaterials> list = iBuildMaterialsService.selectJoinList(BuildMaterials.class, query);
        PageInfo<BuildMaterials> pageInfo = new PageInfo<>(list);
        return SaResult.data(pageInfo);
    }

    @PostMapping("/save")
    @ApiOperation("保存主材料")
    @Transactional(rollbackFor = Exception.class)
    public SaResult save(@RequestBody BuildMaterials buildMaterials) {
        try {
            BuildCategory byId = iBuildCategoryService.getById(buildMaterials.getCategory());

            if (StringUtils.isNotNull(buildMaterials.getId())) {
                //修改情况
                BuildMaterials materials = iBuildMaterialsService.getById(buildMaterials.getId());
                BuildCategory category = iBuildCategoryService.getById(materials.getCategory());
                category.setMaterialsCount(category.getMaterialsCount() - 1);
                iBuildCategoryService.updateById(category);
            }

            //新增情况
            byId.setMaterialsCount(byId.getMaterialsCount() + 1);
            iBuildCategoryService.updateById(byId);

            if (iBuildMaterialsService.saveOrUpdate(buildMaterials)) {
                return SaResult.ok("保存成功");
            } else {
                return SaResult.error("保存失败，请稍后再试");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/del")
    @ApiOperation("删除主材料")
    @Transactional(rollbackFor = Exception.class)
    public SaResult del(Integer id, Integer category) {
        boolean b = false;
        boolean b1 = false;

        try {
            b = iBuildMaterialsService.removeById(id);
            BuildCategory byId = iBuildCategoryService.getById(category);
            byId.setMaterialsCount(byId.getMaterialsCount() - 1);
            b1 = iBuildCategoryService.updateById(byId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (b && b1) {
            return SaResult.ok("删除成功");
        } else {
            return SaResult.error("删除失败，请稍后再试");
        }
    }
}
